package com.example.test8326;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SQLiteActivity extends AppCompatActivity implements View.OnClickListener {

    EditText name, age, height, id;
    Button addDataBtn, showAllBtn, clearShowBtn, deleteAllBtn, deleteIdBtn, selectIdBtn, updateIdBtn;
    TextView showData;
    DBAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        // 初始化控件
        initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 打开数据库
        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        dbAdapter.close();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addData:
                if (!TextUtils.isEmpty(name.getText()) && !TextUtils.isEmpty(age.getText()) && !TextUtils.isEmpty(height.getText())) {
                    People people = new People();
                    people.Name = name.getText().toString();
                    people.Age = Integer.parseInt(age.getText().toString());
                    people.Height = Float.parseFloat(height.getText().toString());
                    dbAdapter.insert(people);
                }
                break;
            case R.id.showAll:
                String result = "";
                People[] peoples = dbAdapter.getAllData();
                for (int i = 0; i < peoples.length; ++i) {
                    result += peoples[i].toString();
                    result += "\n";
                }
                showData.setText(result);
                break;
            case R.id.clearShow:
                showData.setText(null);
                break;
            case R.id.deleteAll:
                dbAdapter.deleteAllData();
                break;
            case R.id.deleteId:
                if (!TextUtils.isEmpty(id.getText())) {
                    dbAdapter.deleteOneData(Long.parseLong(id.getText().toString()));
                }
                break;
            case R.id.selectId:
                if (!TextUtils.isEmpty(id.getText())) {
                    String oneResult = "";
                    People[] people = dbAdapter.getOneData(Long.parseLong(id.getText().toString()));
                    for (int i = 0; i < people.length; ++i) {
                        oneResult += people[i].toString();
                    }
                    showData.setText(oneResult);
                }
                break;
            case R.id.updateId:
                if (!TextUtils.isEmpty(name.getText()) && !TextUtils.isEmpty(age.getText()) &&
                        !TextUtils.isEmpty(height.getText()) && !TextUtils.isEmpty(id.getText())) {
                    People people = new People();
                    people.Name = name.getText().toString();
                    people.Age = Integer.parseInt(age.getText().toString());
                    people.Height = Float.parseFloat(height.getText().toString());
                    dbAdapter.updateOneData(Long.parseLong(id.getText().toString()), people);
                }
                break;
        }
    }

    void initViews() {
        name = (EditText) findViewById(R.id.inputName);
        age = (EditText) findViewById(R.id.inputAge);
        height = (EditText) findViewById(R.id.inputHeight);
        id = (EditText) findViewById(R.id.inputId);
        addDataBtn = (Button) findViewById(R.id.addData);
        addDataBtn.setOnClickListener(this);
        showAllBtn = (Button) findViewById(R.id.showAll);
        showAllBtn.setOnClickListener(this);
        clearShowBtn = (Button) findViewById(R.id.clearShow);
        clearShowBtn.setOnClickListener(this);
        deleteAllBtn = (Button) findViewById(R.id.deleteAll);
        deleteAllBtn.setOnClickListener(this);
        deleteIdBtn = (Button) findViewById(R.id.deleteId);
        deleteIdBtn.setOnClickListener(this);
        selectIdBtn = (Button) findViewById(R.id.selectId);
        selectIdBtn.setOnClickListener(this);
        updateIdBtn = (Button) findViewById(R.id.updateId);
        updateIdBtn.setOnClickListener(this);
        showData = (TextView) findViewById(R.id.showData);
    }
}